package com.hdax.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.gulimall.product.dao.CategoryBrandRelationDao;
import com.hdax.gulimall.product.entity.CategoryBrandRelationEntity;
import com.hdax.gulimall.product.entity.PmsBrandEntity;
import com.hdax.gulimall.product.entity.PmsCategoryEntity;
import com.hdax.gulimall.product.service.CategoryBrandRelationService;
import com.hdax.gulimall.product.service.PmsBrandService;
import com.hdax.gulimall.product.service.PmsCategoryService;
import com.hdax.gulimall.product.vo.BrandVo;
import org.apache.http.client.params.ClientPNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service("CategoryBrandRelationService")

public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao,CategoryBrandRelationEntity> implements CategoryBrandRelationService{


    /**
     * 品牌
     */
    @Autowired
    private PmsBrandService pmsBrandService;


    /**
     * 分类
     * @param categoryBrandRelationEntity
     * @return
     */
    @Autowired
    private PmsCategoryService pmsCategoryService;




    @Override
    public boolean saveDetail(CategoryBrandRelationEntity categoryBrandRelationEntity) {

        Integer brandId = categoryBrandRelationEntity.getBrandId();
        Integer catelogId = categoryBrandRelationEntity.getCatelogId();

        PmsBrandEntity byId = pmsBrandService.getById(brandId);
        PmsCategoryEntity byId1 = pmsCategoryService.getById(catelogId);
        categoryBrandRelationEntity.setBrandName(byId.getName());
        categoryBrandRelationEntity.setCatelogName(byId1.getName());


        boolean save = this.save(categoryBrandRelationEntity);
        //查询详细的名字
        return save;

    }


    /**
     * 更新品牌的名称
     */
    @Override
    public void updateBrand(Integer brandId, String name) {

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        //第一个参数为更新的内容，第二个参数为更新的条件
        this.update(categoryBrandRelationEntity,new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));

    }


    /**
     * 更新分类的name
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {

        this.baseMapper.updateCategory(catId,name);

    }

    @Override
    public List<PmsBrandEntity> getBrandsByCateId(Long catId) {

        List<CategoryBrandRelationEntity> catelog_id = this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<PmsBrandEntity> collect = catelog_id.stream().map(item -> {
            Integer brandId = item.getBrandId();
            PmsBrandEntity byId = pmsBrandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());

        return collect;
    }

}
